<template>
  <div class="house-detail">
    <!-- 背景装饰 -->
    <div class="bg-decoration"></div>
    
    <el-container v-if="house">
      <!-- 头部导航 -->
      <el-header class="detail-header">
        <div class="header-content">
          <el-button @click="goBack" type="primary" plain class="back-btn">
            <el-icon><ArrowLeft /></el-icon>
            返回列表
          </el-button>
          <div class="title-section">
            <h1>{{ house.title }}</h1>
            <div class="status-badge">
              <el-tag v-if="house.status === 'available'" type="success" size="large" effect="light">
                <el-icon><CircleCheck /></el-icon>
                可租赁
              </el-tag>
              <el-tag v-else-if="house.status === 'rented'" type="danger" size="large" effect="light">
                <el-icon><CircleClose /></el-icon>
                已租出
              </el-tag>
              <el-tag v-else-if="house.status === 'maintenance'" type="warning" size="large" effect="light">
                <el-icon><Tools /></el-icon>
                维修中
              </el-tag>
              <el-tag v-else type="info" size="large" effect="light">不可用</el-tag>
            </div>
          </div>
        </div>
      </el-header>
      
      <!-- 主要内容 -->
      <el-main class="detail-main">
        <div class="house-content">
          <!-- 图片展示区域 -->
          <el-card class="image-card" shadow="hover">
            <div class="image-gallery">
              <el-carousel v-if="house.images && house.images.length > 0" 
                         height="500px" 
                         indicator-position="outside"
                         arrow="always"
                         autoplay
                         :interval="5000">
                <el-carousel-item v-for="(media, index) in house.images" :key="index">
                  <div class="media-container">
                    <!-- 图片显示 -->
                    <img 
                      v-if="getFileType(media) === 'image'" 
                      :src="media" 
                      class="gallery-image" 
                    />
                    <!-- 视频显示 -->
                    <div v-else-if="getFileType(media) === 'video'" class="video-container">
                      <!-- 视频缩略图 -->
                      <div class="video-thumbnail-large" @click="playVideoInCarousel($event, index)">
                        <el-icon size="80" class="video-play-icon"><VideoPlay /></el-icon>
                        <div class="video-info">
                          <span class="video-label">点击播放视频</span>
                          <span class="video-format">{{ getFileExtension(media) }}</span>
                        </div>
                      </div>
                      <!-- 隐藏的视频元素，用于预加载 -->
                      <video 
                        :src="media" 
                        class="gallery-video hidden-video" 
                        preload="metadata"
                        :ref="`video-${index}`"
                        controls
                        style="display: none;"
                      >
                        您的浏览器不支持视频播放
                      </video>
                    </div>
                    <div class="media-overlay">
                      <span class="media-counter">{{ index + 1 }} / {{ house.images.length }}</span>
                      <span class="media-type">{{ getFileType(media) === 'video' ? '视频' : '图片' }}</span>
                    </div>
                  </div>
                </el-carousel-item>
              </el-carousel>
              <div v-else class="no-media">
                <el-icon size="80" class="no-media-icon"><Picture /></el-icon>
                <p>暂无媒体文件</p>
              </div>
            </div>
          </el-card>

          <!-- 基本信息卡片 -->
          <el-card class="info-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon class="header-icon"><House /></el-icon>
                <span>房源信息</span>
              </div>
            </template>
            
            <!-- 价格展示 -->
            <div class="price-section">
              <div class="price-container">
                <span class="price">¥{{ house.price }}</span>
                <span class="unit">/月</span>
              </div>
              <div class="price-note">租金不含水电费</div>
            </div>
            
            <!-- 房源规格 -->
            <div class="house-specs">
              <div class="spec-grid">
                <div class="spec-item">
                  <div class="spec-icon">
                    <el-icon><House /></el-icon>
                  </div>
                  <div class="spec-content">
                    <span class="spec-label">建筑面积</span>
                    <span class="spec-value">{{ house.area }}㎡</span>
                  </div>
                </div>
                <div class="spec-item">
                  <div class="spec-icon">
                    <el-icon><HomeFilled /></el-icon>
                  </div>
                  <div class="spec-content">
                    <span class="spec-label">房屋户型</span>
                    <span class="spec-value">{{ house.type }}室</span>
                  </div>
                </div>
                <div class="spec-item">
                  <div class="spec-icon">
                    <el-icon><Location /></el-icon>
                  </div>
                  <div class="spec-content">
                    <span class="spec-label">详细地址</span>
                    <span class="spec-value">{{ house.address }}</span>
                  </div>
                </div>
                <div class="spec-item">
                  <div class="spec-icon">
                    <el-icon><Clock /></el-icon>
                  </div>
                  <div class="spec-content">
                    <span class="spec-label">发布时间</span>
                    <span class="spec-value">{{ formatDate(house.created_at) }}</span>
                  </div>
                </div>
              </div>
            </div>
          </el-card>
          
          <!-- 房源描述 -->
          <el-card class="description-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon class="header-icon"><Document /></el-icon>
                <span>房源描述</span>
              </div>
            </template>
            <div class="description-content">
              <p>{{ house.description || '房东很懒，暂未添加房源描述～' }}</p>
            </div>
          </el-card>
          
          <!-- 房东信息 -->
          <el-card v-if="house.landlord" class="landlord-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon class="header-icon"><User /></el-icon>
                <span>房东信息</span>
              </div>
            </template>
            <div class="landlord-info">
              <div class="landlord-avatar">
                <el-avatar :size="80" class="avatar">
                  {{ house.landlord.username[0].toUpperCase() }}
                </el-avatar>
                <div class="landlord-badge">
                  <el-icon><Trophy /></el-icon>
                  认证房东
                </div>
              </div>
              <div class="landlord-details">
                <h3 class="landlord-name">{{ house.landlord.username }}</h3>
                <p class="landlord-phone">
                  <el-icon><Phone /></el-icon>
                  {{ house.landlord.phone || '暂未提供' }}
                </p>
                <el-button type="primary" class="contact-btn" round>
                  <el-icon><ChatRound /></el-icon>
                  联系房东
                </el-button>
                <el-button 
                  v-if="canEditHouse" 
                  type="warning" 
                  class="edit-btn" 
                  round
                  @click="editHouse"
                >
                  <el-icon><Edit /></el-icon>
                  编辑房源
                </el-button>
              </div>
            </div>
          </el-card>
        </div>
      </el-main>
    </el-container>
    
    <!-- 加载状态 -->
    <div v-else class="loading-container">
      <div class="loading-spinner">
        <el-icon class="loading-icon" size="60"><Loading /></el-icon>
      </div>
      <p>正在加载房源信息...</p>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  ArrowLeft, House, HomeFilled, Location, Phone, Picture, 
  Tools, Loading, CircleCheck, CircleClose, User,
  Document, Trophy, ChatRound, Clock, Edit, VideoPlay
} from '@element-plus/icons-vue'
import { houseApi } from '../api'

const route = useRoute()
const router = useRouter()

const house = ref(null)

// 检查当前用户是否可以编辑房源
const canEditHouse = computed(() => {
  const userRole = localStorage.getItem('userRole')
  const username = localStorage.getItem('username')
  
  // 必须是房东角色且是房源的所有者
  return userRole === 'landlord' && 
         house.value && 
         house.value.landlord && 
         house.value.landlord.username === username
})

// 格式化日期
const formatDate = (date) => {
  const d = new Date(date)
  const now = new Date()
  const diff = now - d
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  if (days === 0) return '今天'
  if (days === 1) return '昨天'
  if (days < 7) return `${days}天前`
  if (days < 30) return `${Math.floor(days / 7)}周前`
  return d.toLocaleDateString()
}

// 返回列表
const goBack = () => {
  router.go(-1)
}

// 编辑房源
const editHouse = () => {
  router.push(`/house/edit/${house.value.id}`)
}

// 获取房源详情
const fetchHouseDetail = async () => {
  try {
    const houseId = route.params.id
    const data = await houseApi.getHouseDetail(houseId)
    house.value = data.house
  } catch (error) {
    ElMessage.error('获取房源详情失败')
    router.push('/')
  }
}

// 获取文件类型
const getFileType = (media) => {
  if (media.endsWith('.jpg') || media.endsWith('.png') || media.endsWith('.jpeg') || media.endsWith('.gif')) {
    return 'image'
  } else if (media.endsWith('.mp4') || media.endsWith('.avi') || media.endsWith('.mov') || media.endsWith('.mkv')) {
    return 'video'
  }
  return null
}

// 获取文件扩展名
const getFileExtension = (media) => {
  try {
    // 移除URL参数
    const cleanUrl = media.split('?')[0]
    const parts = cleanUrl.split('.')
    const ext = parts[parts.length - 1]
    return ext.toUpperCase()
  } catch (error) {
    return 'VIDEO'
  }
}

// 播放视频
const playVideoInCarousel = (event, index) => {
  const container = event.target.closest('.video-container')
  if (container) {
    const thumbnail = container.querySelector('.video-thumbnail-large')
    const video = container.querySelector('.gallery-video')
    
    if (thumbnail && video) {
      thumbnail.style.display = 'none'
      video.style.display = 'block'
      video.play()
    }
  }
}

onMounted(() => {
  fetchHouseDetail()
})
</script>

<style scoped>
.house-detail {
  min-height: 100vh;
  background: #ffffff;
  position: relative;
  overflow-x: hidden;
}

.bg-decoration {
  display: none;
}

.detail-header {
  background: #ffffff;
  border-bottom: 1px solid #e4e7ed;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 10;
}

.header-content {
  max-width: 80%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 20px;
}

.back-btn {
  border-radius: 25px;
  padding: 12px 24px;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  transition: all 0.3s ease;
}

.back-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.4);
}

.title-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.title-section h1 {
  margin: 0;
  color: #303133;
  font-size: 28px;
  font-weight: 700;
  background: linear-gradient(45deg, #409EFF, #667eea);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.status-badge .el-tag {
  padding: 8px 16px;
  border-radius: 20px;
  font-weight: 600;
}

.detail-main {
  padding: 32px 40px;
  max-width: 80%;
  margin: 0 auto;
  position: relative;
  z-index: 5;
  background: #f8f9fa;
  min-height: calc(100vh - 80px);
}

.house-content {
  max-width: 100%;
  margin: 0 auto;
}

/* 卡片通用样式 */
.image-card, .info-card, .description-card, .landlord-card {
  border-radius: 16px;
  border: none;
  margin-bottom: 24px;
  background: #ffffff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  animation: slideInUp 0.6s ease-out;
}

.image-card:hover, .info-card:hover, .description-card:hover, 
.landlord-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.header-icon {
  padding: 8px;
  background: linear-gradient(45deg, #409EFF, #36D1DC);
  color: white;
  border-radius: 50%;
}

/* 图片展示 */
.image-gallery {
  position: relative;
}

.media-container {
  position: relative;
  height: 500px;
  border-radius: 12px;
  overflow: hidden;
}

.gallery-image, .gallery-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.media-overlay {
  position: absolute;
  top: 20px;
  right: 20px;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.media-counter {
  font-size: 14px;
}

.media-type {
  font-size: 12px;
  opacity: 0.8;
  background: rgba(255, 255, 255, 0.2);
  padding: 2px 8px;
  border-radius: 10px;
}

.gallery-video {
  border-radius: 12px;
  outline: none;
}

.gallery-video::-webkit-media-controls-panel {
  background-color: rgba(0, 0, 0, 0.8);
}

.gallery-video::-webkit-media-controls-play-button,
.gallery-video::-webkit-media-controls-volume-slider,
.gallery-video::-webkit-media-controls-timeline {
  filter: brightness(1.2);
}

.no-media {
  height: 500px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa, #e3f0ff);
  border-radius: 12px;
  color: #909399;
}

.no-media-icon {
  margin-bottom: 16px;
  opacity: 0.6;
}

/* 价格展示 */
.price-section {
  text-align: center;
  padding: 32px 0;
  background: linear-gradient(135deg, #667eea, #764ba2);
  border-radius: 16px;
  color: white;
  margin-bottom: 32px;
}

.price-container {
  margin-bottom: 8px;
}

.price {
  font-size: 48px;
  font-weight: 800;
  text-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.unit {
  font-size: 20px;
  opacity: 0.9;
  margin-left: 8px;
}

.price-note {
  font-size: 14px;
  opacity: 0.8;
}

/* 房源规格 */
.spec-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.spec-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  background: linear-gradient(135deg, #f8f9fa, #e9ecef);
  border-radius: 16px;
  border-left: 4px solid #409EFF;
  transition: all 0.3s ease;
}

.spec-item:hover {
  transform: translateX(8px);
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.15);
}

.spec-icon {
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(45deg, #409EFF, #36D1DC);
  color: white;
  border-radius: 50%;
  font-size: 20px;
}

.spec-content {
  flex: 1;
}

.spec-label {
  display: block;
  color: #909399;
  font-size: 14px;
  margin-bottom: 4px;
}

.spec-value {
  display: block;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
}

/* 描述内容 */
.description-content p {
  line-height: 1.8;
  color: #606266;
  font-size: 16px;
  margin: 0;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
  border-left: 4px solid #67C23A;
}

/* 房东信息 */
.landlord-info {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 20px;
}

.landlord-avatar {
  position: relative;
  text-align: center;
}

.avatar {
  background: linear-gradient(45deg, #409EFF, #36D1DC);
  font-size: 24px;
  font-weight: bold;
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.3);
}

.landlord-badge {
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(45deg, #f093fb, #f5576c);
  color: white;
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 12px;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 4px;
}

.landlord-details {
  flex: 1;
}

.landlord-name {
  margin: 0 0 12px 0;
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.landlord-phone {
  margin: 0 0 16px 0;
  color: #606266;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
}

.contact-btn {
  background: linear-gradient(45deg, #67C23A, #85CE61);
  border: none;
  padding: 12px 24px;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(103, 194, 58, 0.3);
  margin-right: 12px;
}

.contact-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(103, 194, 58, 0.4);
}

.edit-btn {
  background: linear-gradient(45deg, #E6A23C, #F56C6C);
  border: none;
  padding: 12px 24px;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(230, 162, 60, 0.3);
}

.edit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(230, 162, 60, 0.4);
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  color: white;
  text-align: center;
}

.loading-spinner {
  margin-bottom: 20px;
}

.loading-icon {
  animation: spin 1s linear infinite;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.3));
}

/* 动画 */
@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .detail-header {
    padding: 16px 0;
  }
  
  .header-content {
    padding: 0 16px;
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }
  
  .title-section {
    width: 100%;
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .title-section h1 {
    font-size: 24px;
  }
  
  .detail-main {
    padding: 16px 20px;
  }
  
  .spec-grid {
    grid-template-columns: 1fr;
  }
  
  .landlord-info {
    flex-direction: column;
    text-align: center;
    gap: 16px;
  }
  
  .price {
    font-size: 36px;
  }
  
  .media-container {
    height: 300px;
  }
  
  .no-media {
    height: 300px;
  }
}

@media (max-width: 480px) {
  .price {
    font-size: 28px;
  }
  
  .title-section h1 {
    font-size: 20px;
  }
  
  .spec-item {
    padding: 16px;
  }
  
  .media-container {
    height: 250px;
  }
}

/* 视频容器样式 */
.video-container {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-thumbnail-large {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.video-thumbnail-large::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at center, transparent 0%, rgba(0,0,0,0.3) 100%);
  z-index: 1;
}

.video-thumbnail-large:hover {
  transform: scale(1.02);
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.4);
}

.video-play-icon {
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.3));
  transition: all 0.3s ease;
  z-index: 2;
  position: relative;
}

.video-thumbnail-large:hover .video-play-icon {
  transform: scale(1.1);
  filter: drop-shadow(0 6px 12px rgba(0,0,0,0.4));
}

.video-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 2;
  position: relative;
}

.video-label {
  font-size: 16px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.video-format {
  font-size: 12px;
  background: rgba(255, 255, 255, 0.2);
  padding: 4px 12px;
  border-radius: 12px;
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.hidden-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style> 